import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * A Servlet to help view jpg images
 *
 * @author James Chy
 */

public class JDBCServlet extends HttpServlet {

    String initpath;
	PrintWriter out;
	String connection;
	ResourceBundle rb;

    static final long serialVersionUID = 0;
    
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        rb = ResourceBundle.getBundle("LocalStrings");
        connection = rb.getString("servletAlbum.connection");
    }
        

    public ResultSet executeQuery(String query)
    {
        ResultSet rs=null;
        try
        {
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            java.sql.Connection conn;
            conn = DriverManager.getConnection(connection);
//                   "jdbc:mysql://localhost/test?user=jimchy&password=lavacat1");
            Statement stmt;

            stmt=conn.createStatement();
            // if(showQuery) out.println("Query: " + query + "<BR>");
                  
            rs = stmt.executeQuery(query);

        }
        catch(ClassNotFoundException cnfe)
        {
            out.println("Class Not Found Exception "+cnfe.toString());
        }
        
        catch(InstantiationException ie)
        {
            out.println("Instantiation Exception "+ie.toString());
        }        
        catch(IllegalAccessException iae)
        {
            out.println("Illegal Access Exception "+iae.toString());
        }
        catch(SQLException sqle)
        {
            out.println("SQL Exception "+sqle.toString());
        }

        return rs;
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();

        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

		String title = "Read from JDBC";

		out.println("<title>" + title + "</title>");  
        out.println("</head>");
        out.println("<body>");

        try
        {
	    	Class.forName("org.gjt.mm.mysql.Driver").newInstance();
            java.sql.Connection conn;
            conn = DriverManager.getConnection(connection);
//		   "jdbc:mysql://localhost/test?user=jimchy&password=lavacat1");
            String query = "SELECT DISTINCT images.image_file " + 
                           "FROM images " +
                           "ORDER BY images.image_file";
            out.println("Query: " + query);

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            
            out.println("<TABLE><TR><TD>Category</TD><TD>Date</TD><TD>Image_File</TD></TR>");
            while (rs.next()) 
            {
                String image_file = rs.getString("images.image_file");
                out.println("<TR><TD>");
                out.println("        <A HREF=\"OneImage?image=" + image_file + "\">");
                out.println("            " + image_file);
                out.println("        </A>");
                out.println("    </TD></TR>"); 
            }
            out.println("</TABLE>");
        }
        catch(ClassNotFoundException cnfe)
        {
            out.println("Class Not Found Exception " + cnfe.getMessage());
        }
        
        catch(InstantiationException ie)
        {
            out.println("Instantiation Exception " + ie.getMessage());
        }	
        catch(IllegalAccessException iae)
        {
            out.println("Illegal Access Exception " + iae.getMessage());
        }
        catch(SQLException sqle)
        {
            out.println("SQL Exception");
        }
        
    out.println("</body>");
    out.println("</html>");

    }
               
}



